package com.lr.server.demo3;

import java.util.HashMap;
import java.util.Map;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class ServletHandler extends DefaultHandler{

	private Map<String,String> mapping;
	
	private Map<String,String> servlet;
	
	private String tag;
	
	private String bigTag;
	
	private String key;
	
	private String value;
	
	@Override
	public void startDocument() throws SAXException {
		super.startDocument();
		mapping = new HashMap<String,String>();
		servlet = new HashMap<String,String>();
	}

	@Override
	public void endDocument() throws SAXException {
		super.endDocument();
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);
//		if(null != qName && (qName.equals("servlet") || qName.equals("servlet-mapping"))){
//			bigTag = qName;
//		}
		if(null != qName && (qName.equals("servlet-name") || qName.equals("servlet-class") || qName.equals("url-pattern"))){
			tag = qName;
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		super.endElement(uri, localName, qName);
		if(null != qName){
	        if(qName.equals("servlet")){
			    servlet.put(key, value);
		    }
		    else if(qName.equals("servlet-mapping")){
			    mapping.put(key, value);
		    }
		}
		tag = null;
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		super.characters(ch, start, length);
		if(null != tag && tag.equals("servlet-name")){
			key = new String(ch,start,length);
		}
		else if(null != tag && (tag.equals("servlet-class") || tag.equals("url-pattern"))){
			value = new String(ch,start,length);
		}
	}

	public Map<String, String> getMapping() {
		return mapping;
	}

	public Map<String, String> getServlet() {
		return servlet;
	}

	
}
